#include "CLinkStack.h"

cLinkNode::cLinkNode()
{
	this->data = NULL;
	this->next = nullptr;
}

cLinkNode::cLinkNode(char a)
{
	this->data = a;
	this->next = nullptr;
}

cLinkNode::cLinkNode(char a, cLinkNode* n)
{
	this->data = a;
	this->next = n;
}

cLinkNode::~cLinkNode()
{
}

void cLinkStack::push(char e)
{
	cLinkNode* p = new cLinkNode(e, this->head->next);
	this->head->next = p;
}

char cLinkStack::pop()
{
	if (this->isEmpty())
	{
		return NULL;
	}
	char c;
	c = this->head->next->data;
	cLinkNode* p = this->head->next;
	this->head->next = p->next;
	delete p;
	return c;
}

bool cLinkStack::isEmpty()
{
	if (this->head->next==nullptr)
	{
		return true;
	}
	else
	{
		return false;
	}
}

char cLinkStack::getTop()
{
	return this->head->next->data;
}

cLinkStack::cLinkStack()
{
	this->head = new cLinkNode();
}

cLinkStack::cLinkStack(string str)
{
	this->head = new cLinkNode();
	cLinkNode* pre, * p;
	p = this->head;
	int i = 0;
	while (str[i]!='\0')
	{
		pre = new cLinkNode(str[i]);
		p->next = pre;
		p = pre;
		i++;
	}
}

cLinkStack::~cLinkStack()
{
	cLinkNode* p, * pre;
	p = this->head;
	pre = p->next;
	while (pre!=nullptr)
	{
		delete p;
		p = pre;
		pre = pre->next;
	}
	delete p;
}

bool isBracketsMatch(string str)
{
	cLinkStack* s = new cLinkStack();
	int i = 0;
	while (str[i]!='\0')
	{
		switch (str[i])
		{
		case '(':
			s->push(str[i]);
			break;
		case ')':
			if (s->getTop()=='(')
			{
				s->pop();
			}
			break;
		case '[':
			s->push(str[i]);
			break;
		case ']':
			if (s->getTop() == '[')
			{
				s->pop();
			}
			break;
		case '{':
			s->push(str[i]);
			break;
		case '}':
			if (s->getTop() == '{')
			{
				s->pop();
			}
			break;
		default:
			break;
		}
		i++;
	}
	if (s->isEmpty())
	{
		return true;
	}
	else
	{
		return false;
	}
}
